package com.aqie.libfactory.data.helper;

import android.text.TextUtils;
import android.util.Log;

import com.aqie.libcommon.factory.data.DataSource;
import com.aqie.libcommon.utils.SimpleJsonTool;
import com.aqie.libfactory.Factory;
import com.aqie.libfactory.model.api.RspModel;
import com.aqie.libfactory.model.api.account.AccountRspModel;
import com.aqie.libfactory.model.api.account.LoginModel;
import com.aqie.libfactory.model.api.user.RegisterModel;
import com.aqie.libfactory.net.Network;
import com.aqie.libfactory.net.RemoteService;
import com.aqie.libfactory.persistence.Account;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * 网络请求 注册逻辑
 */
public class AccountHelper {

    /**
     * 注册的接口，异步的调用
     *
     * @param model    传递一个注册的Model进来
     * @param callback 成功与失败的接口回送
     */
    public static void register(final RegisterModel model, final DataSource.Callback<Boolean> callback) {
        // 调用Retrofit对我们的网络请求接口做代理
        RemoteService service = Network.remote();
        // 得到一个Call
        Call<RspModel<Boolean>> call = service.accountRegister(model);
        // 异步的请求
        call.enqueue(new Callback<RspModel<Boolean>>() {
            @Override
            public void onResponse(Call<RspModel<Boolean>> call, Response<RspModel<Boolean>> response) {
                RspModel<Boolean> rspModel = response.body();
                // 打印消息回送
                String rspString = SimpleJsonTool.toJson(rspModel); Log.e(getClass().getSimpleName(),rspString);
                if (rspModel!=null && rspModel.success()) {
                    callback.onDataLoaded(rspModel.getData());
                }else{
                    Factory.decodeRspCode(rspModel, callback);
                }
            }

            @Override
            public void onFailure(Call<RspModel<Boolean>> call, Throwable t) {

            }
        });

    }

    /**
     * 登录的调用
     *
     * @param model    登录的Model
     * @param callback 成功与失败的接口回送
     */
    public static void login(final LoginModel model, final DataSource.Callback<AccountRspModel> callback) {
        // 调用Retrofit对我们的网络请求接口做代理
        RemoteService service = Network.remote();
        // 得到一个Call
        Call<RspModel<AccountRspModel>> call = service.accountLogin(model);
        // 异步的请求
        call.enqueue(new AccountRspCallback(callback));
    }


    /**
     * 对设备Id进行绑定的操作
     *
     * @param callback Callback
     */
    public static void bindPush(final DataSource.Callback<AccountRspModel> callback) {
        // 检查是否为空
        String pushId = Account.getDeviceId();
        if (TextUtils.isEmpty(pushId))
            return;

        // 调用Retrofit对我们的网络请求接口做代理
        RemoteService service = Network.remote();
        Call<RspModel<AccountRspModel>> call = service.accountBind(pushId);
        call.enqueue(new AccountRspCallback(callback));
    }



    /**
     * 登录请求的回调部分封装
     */
    private static class AccountRspCallback implements Callback<RspModel<AccountRspModel>> {
        final DataSource.Callback<AccountRspModel> callback;

        AccountRspCallback(DataSource.Callback<AccountRspModel> callback) {
            this.callback = callback;
        }

        /**
         * 请求成功返回
         * @param response 返回账户模型,内部使用Gson解析
         */
        @Override
        public void onResponse(Call<RspModel<AccountRspModel>> call, Response<RspModel<AccountRspModel>> response) {
            RspModel<AccountRspModel> rspModel = response.body();
            // 打印消息回送
            // String rspString = SimpleJsonTool.toJson(rspModel); Log.e("TAG",rspString);
            if (rspModel!=null && rspModel.success()){
                // 1. 拿到实体
                AccountRspModel accountRspModel = rspModel.getData();
                // 2. 获取个人信息
                // User user = accountRspModel.getUser();
                // 3. todo 数据库写入
                // DbHelper.save(User.class, user);

                // 4. 缓存绑定 同步到XML持久化中 todo 设备绑定
                Account.login(accountRspModel);
                if (callback != null)
                    callback.onDataLoaded(accountRspModel);
            }else{
                Factory.decodeRspCode(rspModel, callback);
            }
        }

        /**
         * 对设备Id进行绑定的操作
         *
         * @param callback Callback
         */
        public static void bindPush(final DataSource.Callback<AccountRspModel> callback) {
            // 检查是否为空
            String pushId = Account.getDeviceId();
            if (TextUtils.isEmpty(pushId))
                return;

            // 调用Retrofit对我们的网络请求接口做代理
            RemoteService service = Network.remote();
            Call<RspModel<AccountRspModel>> call = service.accountBind(pushId);
            call.enqueue(new AccountRspCallback(callback));
        }

        @Override
        public void onFailure(Call<RspModel<AccountRspModel>> call, Throwable t) {
            Log.e("TAG", t.toString());
            // 网络请求失败
            if (callback != null) {
                callback.onDataNotAvailable("网络错误");
            }
        }
    }


}
